//
//  XMAreaSelectView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/15.
//

#import "XMAreaSelectView.h"
#import "XMHAreaModel.h"

@interface XMAreaSelectView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) NSArray *areaModelArr;

@end

@implementation XMAreaSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"地区选择";
    [self addSubview:self.tbl];
}

- (void)setContentWithAreaModelArr:(NSArray *)areaModelArr {
    _areaModelArr = areaModelArr;
    [self.tbl reloadData];
}

- (void)backAction:(UIButton *)button {
    if (self.viewController.navigationController.viewControllers.count > 1) {
        [self.viewController.navigationController popViewControllerAnimated:YES];
    }
    else {
        [self.viewController dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _areaModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(50.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMAreaSelectCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    XMHAreaModel *areaModel = _areaModelArr[indexPath.row];
    cell.textLabel.text = areaModel.name;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.areaSelectBlock ? self.areaSelectBlock(indexPath.row) : nil;
}

- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tbl.separatorColor = kColorHEX(0xe0e0e0);
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
